<template>
  <el-descriptions
      class="margin-top"
      title="教师"
      border
  >
    <template #extra>
      <el-button @click="dialogFormVisible = true" type="primary" style="margin-right: 10px">新增</el-button>
      <el-dialog v-model="dialogFormVisible" title="新增">
        <el-form :model="form">
          <el-form-item label="教师账号" :label-width="formLabelWidth">
            <el-input v-model="form.account" autocomplete="off" type="textarea"/>
          </el-form-item>
          <el-form-item label="教师密码" :label-width="formLabelWidth">
            <el-input v-model="form.password" autocomplete="off" type="textarea"/>
          </el-form-item>
          <el-form-item label="教师姓名" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off" type="textarea"/>
          </el-form-item>
          <el-form-item label="入职时间" :label-width="formLabelWidth">
            <el-input v-model="form.enrollmentYear" autocomplete="off" type="textarea"/>
          </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="addTeacher">
          新增
        </el-button>
      </span>
        </template>
      </el-dialog>
    </template>
  </el-descriptions>

  <el-table :data="tableData" style="width: 100%">
    <el-table-column label="账号" prop="account" />
    <el-table-column label="密码" prop="password" />
    <el-table-column label="姓名" prop="name" />
    <el-table-column label="入职日期" prop="enrollmentYear" />

    <el-table-column align="right">
      <template #header>
        <el-input v-model="keyword" placeholder="搜索教师"  @keyup.enter.native="search"/>
      </template>
      <template #default="scope">
        <el-button  @click="handleEdit(scope.$index, scope.row)" type="primary" plain>
          编辑
        </el-button>
        <el-button
            type="danger"
            plain
            @click="handleDelete(scope.$index, scope.row)">
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination background layout="prev, pager, next" :total="total" @current-change="load"/>
  <el-dialog v-model="dialogVisible" title="修改教师" draggable>
    <el-form :model="form1">
      <el-form-item label="账号" :label-width="formLabelWidth">
        <el-input v-model="form1.account" autocomplete="off" type="textarea"/>
      </el-form-item>
      <el-form-item label="密码" :label-width="formLabelWidth">
        <el-input v-model="form1.password" autocomplete="off" type="textarea"/>
      </el-form-item>
      <el-form-item label="姓名" :label-width="formLabelWidth">
        <el-input v-model="form1.name" autocomplete="off" type="textarea"/>
      </el-form-item>
      <el-form-item label="入职日期" :label-width="formLabelWidth">
        <el-input v-model="form1.enrollmentYear" autocomplete="off" type="textarea"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="edit">
          修改
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {computed, Ref, ref, onMounted} from 'vue'
import Teacher from "@/class/Teacher";

import {findAllTeacherByPage, findAllTeachers} from "@/http";
import axios from "axios";

const total = ref(100)
const current = ref(1)
const keyword = ref('')
const form: Ref<Teacher> = ref<Teacher>({
  id: "",
  account: "",
  password: "",
  name: "",
  enrollmentYear: "",
});
const form1: Ref<Teacher> = ref<Teacher>({
  id: "",
  account: "",
  password: "",
  name: "",
  enrollmentYear: "",
});

const currentChange = (val: number) => {
  console.log(val)
}

const dialogFormVisible = ref(false)
const dialogVisible = ref(false)
const formLabelWidth = '140px'

const handleEdit = (index: number, row: Teacher) => {
  dialogVisible.value = true
  console.log("当前id:"+row.id)
  form1.value = row
}
const pre_form = ref({
  id: "",
  account: "",
  password: "",
  name: "",
  enrollmentYear: "",
})
const now_form = ref({
  id: "",
  account: "",
  password: "",
  name: "",
  enrollmentYear: "",
})
const edit = () =>{
  now_form.value = form1.value
  pre_form.value = form1.value
  dialogVisible.value = false
  axios.put('/teacher',form1.value).then(function (res){
    console.log("修改的结果"+res)
    load(current.value)
    now_form.value = form1.value
    form1.value = {
      id: "",
      account: "",
      password: "",
      name: "",
      enrollmentYear: "",
    }
  })
  console.log(form1.value.id)
}
const cancel =()=>{
  now_form.value = pre_form.value
  dialogVisible.value = false
  load(current.value)
}

const handleDelete = (index: number, row: Teacher) => {
  axios.delete('/teacher/' + row.id).then(function (res) {
    console.log(res)
    load(current.value)
  })

  console.log(index, row.id)
}

const tableData = ref<any[]>([])

const load = async(val:number)=>{
  let data = await findAllTeacherByPage(10,val-1)
  tableData.value = data.data.content
  current.value = val
  total.value = data.data.totalElements
}

onMounted(async()=>{
  await load(current.value)
} )

const addTeacher = async () => {
  dialogFormVisible.value = false
  await
      axios.post('/teacher',form.value)
          .then(function (res){
            console.log(res.data)
            load(current.value)
            form.value = {
              id: "",
              account: "",
              password: "",
              name: "",
              enrollmentYear: "",
            }
          })
}
const search = () =>{
  axios.get('/teacher/findteacher?name='+keyword.value)
      .then(function(res){
        tableData.value = res.data.data
      })
}
</script>
